package org.blog.service.impl;

import org.blog.dao.CategoryMapper;
import org.blog.entity.Category;
import org.blog.entity.param.CategoryParam;
import org.blog.entity.param.UpdateCategoryParam;
import org.blog.entity.vo.CategoryVO;
import org.blog.service.CategoryService;
import org.blog.utils.ObjectToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public boolean add(Category category) {
        return false;
    }

    @Override
    public List<Category> search(CategoryParam category) {

        try {
            return categoryMapper.getCategory(ObjectToMap.convertBeanToMap(category));
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean delete(Integer categoryId) {
        int i = categoryMapper.deleteByPrimaryKey(categoryId);
        if (i > 0) {
            return true;
        }
        return false;

    }

    @Override
    public boolean update(UpdateCategoryParam param) {
        int i = 0;
        try {
            i = categoryMapper.updateCategory(ObjectToMap.convertBeanToMap(param));
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<CategoryVO> getCategorys(CategoryParam param) {
        Map<String, Object> stringObjectMap = null;
        try {
            stringObjectMap = ObjectToMap.convertBeanToMap(param);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        List<CategoryVO>  list = categoryMapper.getCategorys(stringObjectMap);
        return list;
    }

    @Override
    public Category getCategoryById(Integer categoryId) {
        return categoryMapper.selectByPrimaryKey(categoryId);
    }

    @Override
    public List<Category> getCategory(CategoryParam categoryParam) {
        try {
            return categoryMapper.getCategory(ObjectToMap.convertBeanToMap(categoryParam));
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
